package com.ktkq.action;

import java.util.Map;

import javax.annotation.Resource;
import javax.jms.Session;

import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.ktkq.service.admin.AdminService;
import com.ktkq.service.teacher.TeacherService;
import com.ktkq.util.Pageing;
import com.ktkq.vo.Admin;
import com.ktkq.vo.Teacher;

@SuppressWarnings("serial")
@Controller
public class LoginAction extends BaseAction implements SessionAware{

	Admin admin;
	@SuppressWarnings("rawtypes")
	Map session;
	@Autowired
	AdminService adminService;
	@Resource(name="teacherService") 
	 TeacherService teacherService;
	String resultView;    //结果视图    
	@Override
	public String execute() throws Exception {
		
		
		return INPUT;
	}
	
	@SuppressWarnings("unchecked")
	public String isLogin() {
		if(admin.getRole().equals("00")){
			if(adminService.queryList(admin).size()>0){
				session.put("username", admin.getName());
				session.put("role", adminService.queryList(admin).get(0).getRole());
				resultView = "/admin/login_toAdminMain.action";
				return SUCCESS;
			}
		}else{
			Pageing  pageing=new Pageing();
			pageing.setPageSize(1);
			pageing.setName(admin.getName());
			pageing.setPassword(admin.getPassword());
			if(teacherService.queryList(pageing).getDataList().size()>0){
				session.put("username", admin.getName());
				session.put("teacherID",teacherService.queryList(pageing).getDataList().get(0).getId());
				session.put("role", teacherService.queryList(pageing).getDataList().get(0).getRole());
				resultView = "/admin/login_toMain.action";
				return SUCCESS;
			}	
		}
		resultView = "/main/login.jsp";
		return SUCCESS;
		
	}
	
	public String toMain() throws Exception {
		
		resultView = "/main/index.jsp";
		return SUCCESS;
	}
public String toAdminMain() throws Exception {
		
		resultView = "/admin/index.jsp";
		return SUCCESS;
	}
public String outLogin() throws Exception {
		session.remove("username");
		session.remove("role");
		resultView = "/main/login.jsp";
		return SUCCESS;
	}
	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	public String getResultView() {
		return resultView;
	}

	public void setResultView(String resultView) {
		this.resultView = resultView;
	}

	public TeacherService getTeacherService() {
		return teacherService;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	@SuppressWarnings("rawtypes")
	public Map getSession() {
		return session;
	}

	@SuppressWarnings("rawtypes")
	public void setSession(Map session) {
		this.session = session;
	}
	

}
